package cn.edu.gzu.controller;

import cn.edu.gzu.common.Result;
import cn.edu.gzu.common.ResultCode;
import cn.edu.gzu.entity.VideosEntity;
import cn.edu.gzu.service.VideoService;
import cn.edu.gzu.vo.VideoPageVo;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/video")
@RestController
public class VideoController {
    @Resource(name = "videoService")
    private VideoService videosService;


    @PostMapping("/page")
    public JSONObject getPage(@RequestBody VideoPageVo videoPageVo){
        LambdaQueryWrapper<VideosEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(VideosEntity::getTopic, videoPageVo.getQuery_data()).orderByAsc(VideosEntity::getId);
        IPage<VideosEntity> page = videosService.page(new Page<>(videoPageVo.getPage(), videoPageVo.getSize()), queryWrapper);
        if (videoPageVo.getPage() > page.getPages() && page.getTotal() != 0) {
            return Result.failure(ResultCode.FAILURE_QUERY_PAGE_END_ERROR,page);
        }
        if (page.getTotal() < 1) {
            return Result.failure(ResultCode.FAILURE_QUERY_VIDEO_ERROR,page);
        }
        return Result.success("刷新成功！",page);
    }


    @PostMapping("/info")
    public List<VideosEntity> getVideoInfo(@RequestParam("id") long id){
        QueryWrapper<VideosEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("topic","video_url","sort");
        queryWrapper.eq("video_total_id",id);
        queryWrapper.orderByAsc("sort");
        return  videosService.list(queryWrapper);
    }


}
